package com.kepler.social.usersecurity.dao.mapper;

import com.kepler.mybatis.base.MyBatisBaseMapper;
import com.kepler.social.usersecurity.dao.domain.UserMobilePhone;
import com.kepler.social.usersecurity.dao.domain.UserMobilePhoneExample;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * 如果要通过主键查找，那么需要加上mobilePhone,这个定位对应的表，由sharding完成，id找到行，由mySql完成
 */
@Mapper
public interface UserMobilePhoneMapper extends MyBatisBaseMapper<UserMobilePhone, Long, UserMobilePhoneExample> {


    String Base_Column_List = "id, country_calling_code, phone_number, user_id, `state`, disuse_time, created_time, modified_time";

    @Select(" select " + Base_Column_List +
            " from u_mobile_phone " +
            " where country_calling_code = #{countryCallingCode} and `phone_number` = #{phoneNumber}")
    List<UserMobilePhone> selectByPhoneNumber(@Param("countryCallingCode") int countryCallingCode,
                                              @Param("phoneNumber") long phoneNumber);


    @Select(" select " + Base_Column_List +
            " from u_mobile_phone " +
            " where     country_calling_code = #{countryCallingCode} and `phone_number` = #{phoneNumber}" +
            "       and `state` = #{state}" +
            " limit 1")
    UserMobilePhone selectByState(@Param("countryCallingCode") int countryCallingCode,
                                  @Param("phoneNumber") long phoneNumber, @Param("state") int state);

    @Select(" select " + Base_Column_List +
            " from u_mobile_phone " +
            " where id = #{id} and `phone_number` = #{phoneNumber}" +
            " limit 1")
    UserMobilePhone selectById(@Param("id") long id, @Param("phoneNumber") long phoneNumber);


    @Update(" update u_mobile_phone " +
            " set `state` = #{state},  modified_time = #{modifiedTime} " +
            " where id = #{id} and `phone_number` = #{phoneNumber} and `state` <> #{state} ")
    int updateStateById(@Param("id") long id, @Param("phoneNumber") long phoneNumber, @Param("state") int state,
                        @Param("modifiedTime") long modifiedTime);

}